package com.huazi.www.voice;

import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

/**
 * @description：语音合成测试
 * @author：zt
 * @date：2021/5/19 21:08
 * @versions：v1.0
 */
public class Test {

    //应用ID
    public static final String APP_ID = "24204482";

    //接口钥匙
    public static final String API_KEY = "402HaAUdTQLe356dyBd7YRWn";

    //密钥
    public static final String SECRET_KEY = "tZR9ZCTEEsfAAVQC4SenWBaZO6IGH4ap";

    //设置服务凭证
    public static AipSpeech speech;

    //获得服务凭证
    public static AipSpeech getSpeech() {
        if (speech == null) {
            speech = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        }
        return speech;
    }

    //播放音频
    public static void playMp3(String filePath) {
        //字节输入流
        FileInputStream input = null;
        //增强字节流
        BufferedInputStream buffInput = null;
        //播放音频
        Player player = null;
        try {
            input = new FileInputStream(filePath);
            buffInput = new BufferedInputStream(input);
            try {
                player = new Player(buffInput);
                //播放音频
                player.play();
            } catch (JavaLayerException e) {
                System.err.println();
            }
        } catch (FileNotFoundException e) {
            System.err.println();
        } finally {
            try {
                if (buffInput != null) {
                    buffInput.close();
                }
                if (input != null) {
                    input.close();
                }
            } catch (IOException e) {
                System.err.println();
            }
            if (player != null) {
                player.close();
            }
        }
    }

    //操作
    public static void show(String str, String output) {

        //开启服务
        speech = getSpeech();

        //建立连接的超时事件
        speech.setConnectionTimeoutInMillis(6000);

        //传输数据的超时时间
        speech.setSocketTimeoutInMillis(6000);

        //设置参数
        HashMap<String, Object> map = new HashMap<>();

        //设置音库
        map.put("per", 1);

        //设置语速
        map.put("spd", 5);

        //设置音调
        map.put("pit", 5);

        //设置音量
        map.put("vol", 15);

        //设置log4j日志
        System.setProperty("aip.log4j.conf", "log4j.properties");

        //调用接口
        TtsResponse res = speech.synthesis(str, "zh", 1, map);

        byte[] data = res.getData();

        JSONObject res1 = res.getResult();

        if (data != null) {
            try {
                Util.writeBytesToFileSystem(data, output);
            } catch (IOException e) {
                System.err.println();
            }
        }
        if (res1 != null) {
            System.err.println();
        }

        //调用计算机的音频播放
        playMp3(output);

    }


    //测试
    public static void main(String[] args) {
        //展示
//        show("请汪成杰到问诊室问诊", "D://音频//out.mp3");
    }

}
